function writeError(location, message)
{		
	location = location.concat("_warning_box");	
	var x = document.getElementById(location);
	x.innerHTML = "<a style=\"color:yellow\">" + message + "</a>";	
}


	function clearError(location)
{	
	location = location.concat("_warning_box");
	var x = document.getElementById(location);	
	x.innerHTML = "";

}
	
	
	function is_this_a_number(theString)
{ 
	    /*
	    	abe, 31jan2011/1046pm: imported function from my CS150 project,
	    	Basically scans the passed string if it contains a non-numeric char, if 
	    	so return false
	    */	    
       var x, y=theString.length;
       
       for(x=0;x<y;x++){
          if(theString[x] < '0' || theString[x] > '9') return false;
       }
       return true;
}

function is_this_a_student_number(theString)
{              
       /*
       	abe, 9feb2011/0533pm: oh well, just check if the first four char and last five chars are numbers.
       	if not then invalid
       */      
       return (is_this_a_number (theString.substring(0,4)) && is_this_a_number (theString.substring( 5 )) );
}

	function checkStudentNumber(theString)
{
		
		var x = theString.indexOf('-'), y ;
		var hyphenized;
		
		clearError("student_number");		
		if(theString.length<9)
		{
			writeError("student_number", "Solely 9 digits with or without dash.");
			return false;
		}
		if(x!=-1) 				//dash detected
		{			
				if( is_this_a_student_number(theString) ) {	
					return true; 
				}else
				{
					writeError("student_number", "Non-numeric character detected/Dash wrongly placed.");
					return false;
				}
			
		}else{					//dash not detected

				hyphenized = theString.substring(0,4).concat("-", theString.substring(4) );								
				if(is_this_a_student_number(hyphenized) )
				{ 
					y = document.getElementsByName('student_number');
					y[0].value = hyphenized;
					return true; 
				}else
				{
					writeError("student_number", "Non-numeric character detected");
					return false;
				}
		}

}

function isNameAppropriate(theString, location)
{
		var x, strLen = theString.length;
		var allowable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-. ";
		
		clearError(location);
		/*  21FEB2011/0212a
			To accomodate request of client na pwedeng walang ieenter ng name			
			, 'return false' was turned into comment
		*/
		if( strLen < 2)
		{
			writeError(location, "It is best if this is not blank.");
			//return false;
		}
		for(x=0;x<strLen;x++)
		{
			if(allowable.indexOf(theString[x]) == -1)
			{
				writeError(location, "Only letters, dashes and dots are allowed.");
				return false;
			}
		}

		return true;
}

function isTextAppropriate(theString, location)
{
//		clearError(location); -- must be done in the caller function
		clearError(location);
		var x, strLen = theString.length;
		var allowable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-., 1234567890_?!()/\\";
		
		for(x=0;x<strLen;x++)
		{
			if(allowable.indexOf(theString[x]) == -1)
			{
				writeError(location, "Inappropriate character detected.");				
				return false;
			}
		}
		return true;
}

function isTextPureLetters(theString, location)
{
//		clearError(location); -- must be done in the caller function
		var x, strLen = theString.length;
		var allowable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		
		for(x=0;x<strLen;x++)
		{
			if(allowable.indexOf(theString[x]) == -1)
			{
				writeError(location, "Inappropriate character detected.");				
				return false;
			}
		}
		return true;
}

function isEmailAddressCharactersAcceptable(theString)
{
		var x, strLen = theString.length;
		var allowable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_.@-1234567890";
		
		for(x=0;x<strLen;x++)
		{
			if(allowable.indexOf(theString[x]) == -1)
			{ 
				writeError("email_address", "Only '@', numbers, letters, dashes, underscores and dots are allowed.");
				return false;
			}
		}
		
		if(theString.indexOf('@') == -1)
		{
			writeError("email_address", "Wrong e-mail address format.");
			return false;
		}
		return true;

}

function checkFirstName(theString)
{		
		return isNameAppropriate(theString, "first_name");
}	

function checkMiddleName(theString)
{		
		return isNameAppropriate(theString, "middle_name");
}	

function checkLastName(theString)
{				
		return isNameAppropriate(theString, "last_name");
}	

function checkReligion(theString)
{
		var x, strLen = theString.length;
	
		clearError("religion");
		/*
			// 27feb2011/0534pm: turned into comment to accomodate request na pwedeng magadd na stud num lang
			
		if( strLen < 3)
		{
			writeError("religion", "At least 3 characters are required.");						
			return false;
		}*/
		return isTextAppropriate(theString, "religion");		
}

function checkCourse(theString)
{
		var x, strLen = theString.length;
	
		clearError("course");
		/*
			// 27feb2011/0534pm: turned into comment to accomodate request na pwedeng magadd na stud num lang
		
		if( strLen < 3)
		{
			writeError("course", "At least 3 characters are required.");
			return false;
		}*/
		return isTextPureLetters(theString, "course");		
}

function checkHomeAddress(theString)
{
		var x, strLen = theString.length;
	
		clearError("home_address");
		/*
			// 27feb2011/0534pm: turned into comment to accomodate request na pwedeng magadd na stud num lang
		
		if( strLen < 10)
		{
			writeError("home_address", "At least 10 characters are required.");
			return false;
		}
		*/
		return isTextAppropriate(theString, "home_address");

}

function checkTelephoneNumber(theString, location)
{
		var x, strLen = theString.length;
	
		clearError(location);
		/*
			// 27feb2011/0534pm: turned into comment to accomodate request na pwedeng magadd na stud num lang
		
		if( strLen < 7)
		{
			writeError(location, "At least 7 characters are required.");
			return false;
		}*/
		if (! is_this_a_number(theString) )
		{
			writeError(location, "Numbers only.");
			return false;
		}else return true;
}

function checkEmailAddress(theString)
{
		var x, strLen = theString.length;
		var otherHalf = "";
		var arrayTest;
		
		clearError("email_address");
		
			// 27feb2011/0534pm: changed to accomodate request na pwedeng magadd na stud num lang
		if( strLen == 0) return true;
		
		if( strLen < 6)
		{
			writeError("email_address", "At least 6 characters are required.");
			return false;
		}
		
		if(	isEmailAddressCharactersAcceptable(theString) == false )
		{			
			return false;
		}
		x = theString.indexOf('@');				//find the index of '@' sign
		otherHalf = theString.substring(x+1);   //get the rest of the string proceeding '@'
		arrayTest = otherHalf.split('.');      //split with '.' as delimiter
		if(arrayTest.length < 2)			  //if array length is only one, means there's no dot			
		{
			writeError("email_address", "Wrong e-mail address format.");
			return false;
		}else return true;								
}

function displayErrorAttention()
{
		var x = document.getElementById('errorMessage');
	    x.innerHTML = "<p style=\"background-color: yellow; font-size: 14; color: red\"> Some invalid values are found on some fields. Please correct them.";
	    x.focus();
	    getfocus();
}

function getfocus()
{
	document.getElementById('myAnchor').focus();
}


function checkFatherName(theString)
{		
		return isNameAppropriate(theString, "name_of_father");
}	

function checkMotherName(theString)
{		
		return isNameAppropriate(theString, "name_of_mother");
}	

function checkGuardianName(theString)
{		
		return isNameAppropriate(theString, "name_of_guardian");
}	

function checkFatherContactNumber(theString)
{
	    return checkTelephoneNumber(theString, "telnumber_of_father");
}

function checkMotherContactNumber(theString)
{
	    return checkTelephoneNumber(theString, "telnumber_of_mother");
}

function checkGuardianContactNumber(theString)
{
	    return checkTelephoneNumber(theString, "telnumber_of_guardian");
}

function isDateOk(year, month, day, location)
{
	var myDate;
	var allowedAge = 15;
	var manufacturingYear = 2011;
	var minYearAllowed = 1912;
	var currentYear;
	var lastFebruary = 28;
	
	myDate = new Date();
	currentYear = myDate.getFullYear();

	year = Number(year);
	month = Number(month);
	day = Number(day);
	
	if( currentYear < manufacturingYear || currentYear < year)
	{
		alert("Critical Error! Please check the clock of your computer.");
		return false;
	}
	if( (location == "birthday") && (currentYear - year) < allowedAge ) 
	{
		//writeError(location, "Too young to be a college student.");
		return false;
	}
	if(currentYear < minYearAllowed || year > currentYear)
	{	
		//writeError(location, "Invalid year.");
		return false;
	}
	if( day < 1 )return false;
	switch(Number(month))
	{
		case 1: return (! (day > 31 ));				
		case 2: if ( (year % 4) == 0)		
				{				//leap year
					lastFebruary = 29;
				}			
				return (! (day > lastFebruary) );
		case 3: return (! ( day > 31 ) );				
		case 4: return (! ( day > 30 ) );				
		case 5: return (! ( day > 31 ) );				
		case 6: return (! ( day > 30 ) );				
		case 7: return (! ( day > 31 ) );				
		case 8: return (! ( day > 31 ) );				
		case 9: return (! ( day > 30 ) );				
		case 10: return (! ( day > 31 ) );				
		case 11: return (! ( day > 30 ) );				
		case 12: return (! ( day > 31 ) );				
		default: return false;
	}
}

function checkBirthday(theString)
{
		/*  YYYY/MM/DD
			0123456789
		*/
		var year;
		var month;
		var day;
		
		clearError("birthday");
		/*
			// 27feb2011/0534pm: changed to accomodate request na pwedeng magadd na stud num lang
		*/
		if(theString.length == 0){       
			//writeError("birthday", "Should be strictly 10 characters of numbers and forward back slashes.");
		 	return true;
		}		
		
		if( ! (theString[4] == '/' && theString[7] == '/') )
		{
		 	writeError("birthday", "No or misplaced back slash.");
		 	return false;
		}
		
		year = theString.substring(0,4);
		month = theString.substring(5,7);
		day = theString.substring(8);
		//alert(year + " " + month + " " + day);
		if ( isDateOk(year, month, day, "birthday") == false )
		{
			writeError("birthday", "Invalid date or student too young.");
			return false;
		}
		return true;
}